<template>
    <div class="addProject reset">
        <div class="resetForm zll-form">
            <el-form :model="resetForm" :rules="rules" ref="resetForm">
                <el-form-item class="formList" prop="oldPassword" label="原密码：">
                    <el-input clearable class="input_right" placeholder="请输入原密码"  type="password" v-model="resetForm.oldPassword"></el-input>
                </el-form-item>
                <el-form-item class="formList" prop="password" label="修改密码：">
                    <el-input clearable class="input_right" placeholder="请输入修改密码"  type="password" v-model="resetForm.password"></el-input>
                </el-form-item>
                <el-form-item class="formList" prop="againPassword" label="确认密码：">
                    <el-input clearable class="input_right" placeholder="请确认密码"  type="password" v-model="resetForm.againPassword"></el-input>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
	import {getStore} from "@/plugins/localStore";
    export default {
        data() {
            var checkPass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.resetForm.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                resetForm: {
					oldPassword: "",
					password: "",
					againPassword: ""
                },
                rules: {
					oldPassword: [
                        { required: true, message: "请输入原密码", trigger: "blur" }
                    ],
					password: [
                        { required: true, message: "请输入修改密码", trigger: "blur" }
                    ],
					againPassword: [
                        { required: true, validator: checkPass, trigger: "blur" }
                    ]
                }
            };
        },
        methods: {
            setResetFormData(formName) {
            	let self = this;
				self.$refs[formName].validate(valid => {
                    if (valid) {
						self.updatePassword();
                    } else {
                        return false;
                    }
                });
            },
			updatePassword() {
				let self = this;
				let user = getStore();
				let param = Object.assign({},self.resetForm);
				param.id = user.id;
				self.$http({
					url: "/jinl/login/updatePassword",
					method: "post",
					params:param
				}).then(resp => {
					if (resp.success) {
						self.$emit("resetForm", this.resetForm);
						self.$message.success('修改成功');
						self.$router.push({name: 'login'});
					}else{
						self.$message.error(resp.result);
					}
				});
			},
        }
    };
</script>
<style lang="scss" scoped>
@import "@/style/dialog.scss";
</style>
